package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.sbt.api.TrackApi;
import com.kgc.sbt.domain.Track;
import com.kgc.sbt.mapper.TrackMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created on 2021/6/23 0023.
 * <p>
 * Author: px
 * <p>
 * Description: 足迹操作
 */
@Service
public class TrackProvider implements TrackApi {

    @Autowired
    private TrackMapper trackMapper;



    /**
     * @Author px
     * @Date 2021/6/28 0028
     * @Param [userId]
     * @Description: 查询足迹列表
     **/
    @Override
    public List<Track> queryTrack(Integer userId) {
        return trackMapper.selectById(userId);
    }

    /**
     * @Author px
     * @Date 2021/6/24 0024
     * @Param [userId, goodsId]
     * @Description: 添加足迹
     **/
    @Override
    public Boolean addTrack(Integer userId, Integer goodsId) {

        // 先判断是否存在该条足迹
        Integer isExist = trackMapper.selectTrackByUIdAndGoodsId(userId, goodsId);

        // 若存在，更新最后浏览时间
       if (isExist != 0){
            return trackMapper.updateLookTime(userId, goodsId, new Date()) > 0;
        } else {
            // 若不存在，插入一条新记录
            Track track = Track.builder().userId(userId).goodsId(goodsId).createTime(new Date()).lookTime(new Date()).build();
            return trackMapper.insert(track) > 0 ? true : false;
        }

    }


    /**
     * @Author px
     * @Date 2021/6/24 0024
     * @Param [userId, goodsId]
     * @Description: 删除足迹
     **/
    @Override
    public Boolean delTrack(Integer userId, Integer goodsId) {
        return trackMapper.deleteTrackByUIdAndGoodsId(userId, goodsId) > 0;
    }

}
